using Framework.Core.Io;

namespace Framework.Core.Process.Tasks
{
    internal static class Sc
    {
        private const string ScName = "sc";
        private const string ScPath = @"C:\Windows\System32\sc.exe";
        private const bool ScIsNative = true;

        internal struct ScStart : ICommand
        {
            public ScStart(string name)
                : this()
            {
                Name = ScName;
                FileName = ScPath;
                Arguments = string.Format("start {0}", name);
            }

            public string Name { get; private set; }
            public string FileName { get; private set; }
            public string Arguments { get; private set; }
            public bool IsNative { get { return ScIsNative; } }
        }

        internal struct ScDelete : ICommand
        {
            public ScDelete(string serviceName)
                : this()
            {
                Name = ScName;
                FileName = ScPath;
                Arguments = string.Format(@"delete ""{0}""", serviceName);
            }

            public string Name { get; private set; }
            public string FileName { get; private set; }
            public string Arguments { get; private set; }
            public bool IsNative { get { return ScIsNative; } }
        }

        internal struct ScCreate : ICommand
        {
            public ScCreate(string serviceName,
                string displayName,
                string binPath,
                string arguments,
                string user = "",
                string pwd = "")
                : this()
            {
                Name = ScName;
                FileName = ScPath;
                Arguments = GetCommand(serviceName, displayName, binPath, arguments, user, pwd);
            }

            private static string GetCommand(string serviceName, string displayName, string binPath, string arguments, string user, string pwd)
            {
                var command = string.Format(@"create ""{0}"" binPath= ""{1} {2}"" displayname= ""{3}"" start= auto",
                    serviceName,
                    binPath,
                    arguments,
                    displayName);

                if (!string.IsNullOrEmpty(user))
                {
                    command = string.Format(@"{0} obj= ""{1}"" password= ""{2}""", command, user, pwd);
                }

                return command;
            }

            public string Name { get; private set; }
            public string FileName { get; private set; }
            public string Arguments { get; private set; }
            public bool IsNative { get { return ScIsNative; } }
        }
    }
}